<cfcomponent output="false">

	<cfset this.name = "TIDTool" />
	<cfset this.applicationTimeout = createTimeSpan(5, 0, 0, 0) />
	<cfset this.sessionManagement = true />
	<cfset this.sessionTimeout = createTimeSpan(0, 0, 60, 0) />
	<cfset this.clientManagement = false />
	<cfset this.loginStorage = "session" />
	<cfset this.datasource = "test" />
	<cfset this.mappings["/scrolls"] = getDirectoryFromPath(getCurrentTemplatePath()) />
	<cfset this.message = "" />

	<cffunction name="onApplicationStart" returntype="boolean" access="public" output="false">

		<cfset application.serverRoot = "http://localhost/" />

		<cfreturn true />

	</cffunction>

	<cffunction name="onRequestStart" returntype="void" access="public" output="false">
		<cfargument type="string" name="targetPage" required="true" />
		<cfset currentTemplateName = #GetFileFromPath("#ExpandPath("*.*")#/#arguments.targetPage#")# />
		<cfif structKeyExists(url, "init") and isBoolean(url.init) and url.init>
			<cfset applicationStop() />
			<cflocation url="#arguments.targetPage#" addtoken="false" />
		</cfif>
		<cfif isUserInRole("pending")>
			<cfinclude template="index.cfm" />
		</cfif>
		<cfif isDefined("form.logout")>
	        <cflogout />
	    </cfif>

	    <cflogin>
	        <cfif compareNocase(currentTemplateName, "registrationForm.cfm") eq 0>
	            <cfinclude template="registrationForm.cfm" />
	        	<cfabort />
	        <cfelseif compareNoCase(currentTemplateName, "accessDenied.cfm") eq 0>
	        	<cfinclude template="accessDenied.cfm" />
	        	<cfabort />
	         <cfelseif compareNoCase(currentTemplateName, "recoverPassword.cfm") eq 0>
	        	<cfinclude template="recoverPassword.cfm" />
	        	<cfabort />
	        <cfelseif not isDefined("cflogin")>
	        	<cfinclude template="loginform.cfm" />
	            <cfabort />
	        <cfelse>
	            <cfif cflogin.name is "" or cflogin.password is "">
	                <cfset message = "Benutzername oder Passwort fehlt" />
	                <cfinclude template="loginform.cfm" />
	                <cfabort />
	            <cfelse>
	                <cfquery name="loginQuery" dataSource="tidtool">
		                SELECT Username, Roles
		                FROM Person
		                WHERE
	                   		Username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#cflogin.name#" />
	                    AND
	                    	Password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#hash(cflogin.password)#" />
	                </cfquery>

	                <cfif loginQuery.Roles neq "">
	                    <cfloginuser name="#cflogin.name#" password="#hash(cflogin.password)#" roles="#loginQuery.Roles#" />
	                <cfelse>
	                    <cfset message="Benutzername oder Passwort ist falsch.<br />Bitte nochmal versuchen..." />
	                    <cfinclude template="loginform.cfm" />
	                    <cfabort />
	                </cfif>
	            </cfif>
	        </cfif>
	    </cflogin>

	</cffunction>

	<!--- <cffunction name="onError">
	    <cfargument name="Exception" required="true" />
	    <cfargument type="String" name="EventName" required="true" />
	    <!--- Log all errors. --->
	    <cflog file="#This.Name#" type="error"
	            text="Event Name: " >
	    <cflog file="#This.Name#" type="error"
	            text="Message: ">

	    <!--- Display an error message if there is a page context. --->
	    <cfif NOT (Arguments.EventName IS "onSessionEnd") OR
	            (Arguments.EventName IS "onApplicationEnd")>
	        <cfoutput>
		        <div style="text-align:center;color:red;border:1px solid black;padding: 10px;">
		            <h3>MOEP! Ein unerwarteter Fehler ist aufgetreten.</h3>
		            <p>Wahrscheinlich hast du irgendwas unerlaubtes versucht...</p>
		            <p>
			            Event: #Arguments.Eventname#<br />
						Message: #Arguments.Exception.message#
					</p>
				</div>
	        </cfoutput>
	    </cfif>
	</cffunction> --->
</cfcomponent>